package tools

import (
	"context"
	baseerrors "errors"
	"time"
)

type ContextWithoutDeadline struct {
	ctx context.Context
}

func NewContextWithoutDeadline(ctx context.Context) context.Context {
	return ContextWithoutDeadline{ctx}
}

func (ContextWithoutDeadline) Deadline() (time.Time, bool) { return time.Time{}, false }
func (ContextWithoutDeadline) Done() <-chan struct{}       { return nil }
func (ContextWithoutDeadline) Err() error                  { return nil }

func (l ContextWithoutDeadline) Value(key interface{}) interface{} {
	return l.ctx.Value(key)
}

func CtxStringValue(ctx context.Context, key string) (string, error) {
	value, ok := ctx.Value(key).(string)
	if !ok {
		return "", baseerrors.New("get string value failed")
	}
	return value, nil
}

func UserIdFromCtx(ctx context.Context) (string, error) {
	uid, err := CtxStringValue(ctx, "uid")
	if err != nil {
		return "", baseerrors.New("获取用户ID失败")
	}
	return uid, nil
}
